﻿using System;
using System.Data;
using System.IO;
using System.Xml;
using ESpace.DalTools.Contracts;
namespace ESpace.DalTools.SQLite
{
	public class EntityBuilderSQLite : BuilderBaseSQLite, IEntityBuilder
	{


		#region 属性定义
		private string _DataEntityNameSpace;
		private string _BusEntityNameSpace;
		private string _DataEntitySavePath;
		private string _BusEntitySavePath;
		private string _HbmSavePath;
		private string _TableName;
		private XmlDocument xmlDoc;

		/// <summary>
		/// 数据实体的名称空间
		/// </summary>
		public string DataEntityNameSpace
		{
			get
			{
				return _DataEntityNameSpace;
			}
			set
			{
				_DataEntityNameSpace = value;
			}
		}

		/// <summary>
		/// 业务实体的名称空间
		/// </summary>
		public string BusEntityNameSpace
		{
			get
			{
				return _BusEntityNameSpace;
			}
			set
			{
				_BusEntityNameSpace = value;
			}
		}

		/// <summary>
		/// 数据实体保存路径
		/// </summary>
		public string DataEntitySavePath
		{
			get
			{
				return _DataEntitySavePath;
			}
			set
			{
				string sSplit = value.Substring(value.Length - 1);
				if (sSplit != @"\" && sSplit != "/")
					_DataEntitySavePath = value + @"\";
				else
					_DataEntitySavePath = value;
			}
		}

		/// <summary>
		/// 业务实体保存路径
		/// </summary>
		public string BusEntitySavePath
		{
			get
			{
				return _BusEntitySavePath;
			}
			set
			{
				string sSplit = value.Substring(value.Length - 1);
				if (sSplit != @"\" && sSplit != "/")
					_BusEntitySavePath = value + @"\";
				else
					_BusEntitySavePath = value;
			}
		}


		/// <summary>
		/// 配置文件保存路径
		/// </summary>
		public string HbmSavePath
		{
			get
			{
				return _HbmSavePath;
			}
			set
			{
				string sSplit = value.Substring(value.Length - 1);
				if (sSplit != @"\" && sSplit != "/")
					_HbmSavePath = value + @"\";
				else
					_HbmSavePath = value;
			}
		}

		/// <summary>
		/// 要生成实体的表名，必须先设置此属性才能生成
		/// </summary>
		public string TableName
		{
			get
			{
				return _TableName;
			}
			set
			{
				_TableName = value;
				InitMetaData();
			}
		}

		private bool _WcfDataContract;
		public bool WcfDataContract
		{
			get
			{
				return _WcfDataContract;
			}
			set
			{
				_WcfDataContract = value;
			}
		}
		#endregion

		/// <summary>
		/// 初始化
		/// </summary>
		private void InitMetaData()
		{
			DataTable dtColumns = GetColumns(_TableName);


			DataSet dsMetaData = new DataSet("Schema");
			dsMetaData.Tables.Add(dtColumns);

			xmlDoc = new XmlDocument();
			xmlDoc.LoadXml(dsMetaData.GetXml());

#if DEBUG
			xmlDoc.Save(@"c:\SQLColumnsSchema.xml");
#endif

			XmlElement xmlElement = xmlDoc.DocumentElement;
			xmlElement.SetAttribute("TableName", _TableName);
			xmlElement.SetAttribute("BusEntityNameSpace", _BusEntityNameSpace);
			xmlElement.SetAttribute("DataEntityNameSpace", _DataEntityNameSpace);
			xmlElement.SetAttribute("WcfDataContract", _WcfDataContract.ToString());
		}



		/// <summary>
		/// 创建配置文件
		/// </summary>
		public void BuilderHbmXml()
		{
			if (!Directory.Exists(_HbmSavePath))
				Directory.CreateDirectory(_HbmSavePath);

			string sFilePath = null;
			string sXsltPath = null;
			
			sFilePath = _HbmSavePath + _TableName + ".hbm.xml";
			sXsltPath = GetCurrentExecutePath() + @"Template\Nhibernate\hbm.xslt";
			

			TemplateTransform.TransformXslt(xmlDoc, sXsltPath, sFilePath);
		}

		/// <summary>
		/// 创建数据实体类
		/// </summary>
		public void BuilderDataEntity()
		{
			// 检查目标目录是否存在
			if (!Directory.Exists(_DataEntitySavePath))
				Directory.CreateDirectory(_DataEntitySavePath);

			string sFilePath = null;
			string sXsltPath = null;
			
			sFilePath = _DataEntitySavePath + _TableName + ".cs";
			sXsltPath = GetCurrentExecutePath() + @"Template\Nhibernate\DataEntity.xslt";
			
			TemplateTransform.TransformXslt(xmlDoc, sXsltPath, sFilePath);
		}

	}
}
